<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .location_box {
            background-color: #e62442;
            padding-bottom: 10px;
        }
        .location_box .location {
            display: -webkit-box;
            -webkit-box-align: center;
            -webkit-box-pack: center;
            margin: auto;
            padding: 15px 0;
        }
        .location_box .location img {
            height: 17px;
            display: block;
        }
        .location_box .location span {
            color: #fff;
            font-size: 17px;
            margin-left: 7px;
            display: block;
        }
        .search_wrap {
            width: 100%;
            padding: 0 15px;
            box-sizing: border-box;
            position: absolute;
            top: -15px;
            z-index: 999;
        }
        .search_box {
            height: 31px;
            line-height: 31px;
            background-color: #fff;
            color: #999;
            font-size: 12px;
            border-radius: 5px;
            box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, 0.2);
            display: -webkit-box;
            -webkit-box-align: center;
            -webkit-box-pack: center;
        }
        .search_box img {
            width: 13px;
            display: block;
            margin-right: 6px;
        }
        .search_box input::-webkit-input-placeholder {
            color: #999;
        }
        .search_fixed {
            position: fixed;
            top: 0;
            background-color: #e62442;
            padding: 30px 15px 9px;
            transition: all .3s;
            -webkit-transition: all .3s;
        }
        .banner {
            height: 167px;
            position: relative;
        }
        .banner .swiper-container {
            height: 100%;
        }
        .banner .swiper-container img {
            width: 100%;
            height: 100%;
        }
        .nav {
            padding: 20px 6px 8px;
            background-color: #fff;
            margin-bottom: 6px;
        }
        .nav ul {
            overflow: hidden;
        }
        .nav ul li {
            width: 25%;
            float: left;
            margin-bottom: 12px;
        }
        .nav ul li .li_box {
            text-align: center;
        }
        .nav ul li .li_box .img_box {
            width: 48px;
            height: 48px;
            border-radius: 50%;
            overflow: hidden;
            background-size: 100% 100%;
            background-repeat: no-repeat;
            margin: auto;
            display: -webkit-box;
            -webkit-box-align: center;
            -webkit-box-pack: center;
        }
        .nav ul li .li_box .img_box img {
            display: block;
        }
        .nav ul li:nth-child(1) .li_box .img_box {
            background-image: url('../image/nav_bg1.png');
        }
        .nav ul li:nth-child(1) .li_box .img_box img {
            width: 21px;
        }
        .nav ul li:nth-child(2) .li_box .img_box {
            background-image: url('../image/nav_bg2.png');
        }
        .nav ul li:nth-child(2) .li_box .img_box img {
            width: 28px;
        }
        .nav ul li:nth-child(3) .li_box .img_box {
            background-image: url('../image/nav_bg3.png');
        }
        .nav ul li:nth-child(3) .li_box .img_box img {
            width: 21px;
        }
        .nav ul li:nth-child(4) .li_box .img_box {
            background-image: url('../image/nav_bg4.png');
        }
        .nav ul li:nth-child(4) .li_box .img_box img {
            width: 24px;
        }
        .nav ul li:nth-child(5) .li_box .img_box {
            background-image: url('../image/nav_bg5.png');
        }
        .nav ul li:nth-child(5) .li_box .img_box img {
            width: 27px;
        }
        .nav ul li:nth-child(6) .li_box .img_box {
            background-image: url('../image/nav_bg6.png');
        }
        .nav ul li:nth-child(6) .li_box .img_box img {
            width: 26px;
        }
        .nav ul li:nth-child(7) .li_box .img_box {
            background-image: url('../image/nav_bg7.png');
        }
        .nav ul li:nth-child(7) .li_box .img_box img {
            width: 24px;
        }
        .nav ul li:nth-child(8) .li_box .img_box {
            background-image: url('../image/nav_bg8.png');
        }
        .nav ul li:nth-child(8) .li_box .img_box img {
            width: 23px;
        }
        .nav ul li .li_box .p_box {
            color: #333;
            font-size: 13px;
            margin-top: 6px;
        }
        .discount_box {
            background-color: #fff;
            padding: 0 15px 10px;
            margin-bottom: 6px;
        }
        .discount_box .discount_boxt {
            height: 40px;
            line-height: 40px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .discount_box .discount_boxt img {
            height: 14px;
            display: block;
            margin-right: 5px;
        }
        .discount_box .discount_boxt span {
            display: block;
            font-size: 15px;
            color: #333;
        }
        .discount_box .discount_boxb {
            overflow: hidden;
        }
        .discount_box .discount_boxb .discount_div {
            width: 50%;
            float: left;
            padding: 0 5px 0 0;
            position: relative;
            box-sizing: border-box;
        }
        .discount_box .discount_boxb .discount_div:last-child {
            padding: 0 0 0 5px;
        }
        .discount_box .discount_boxb .discount_div .discount_img {
            width: 100%;
            height: 92px;
        }
        .discount_box .discount_boxb .discount_div .discount_img img {
            width: 100%;
            object-fit: cover;
            height: 100%;
        }
        .discount_box .discount_boxb .discount_div .discount_p {
            width: 60px;
            height: 16px;
            line-height: 16px;
            font-size: 13px;
            color: #fff;
            background-color: #e62442;
            text-align: center;
            position: absolute;
            left: 0;
            top: 0;
        }
        .discount_box .discount_boxb .discount_div:last-child .discount_p {
            background-color: #ff8003;
            left: 5px;
        }
        .screen_box {
            height: 40px;
            background-color: #fff;
            border-bottom: 1px solid #e8e8e8;
            display: -webkit-box;
            display: -webkit-flex;
            align-items: center;
        }
        .screen_box .location_p {
            padding-left: 15px;
            padding-right: 20px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .screen_box .location_p img {
            height: 14px;
            display: block;
            margin-right: 5px;
        }
        .screen_box .location_p span {
            display: block;
            font-size: 15px;
            color: #333;
        }
        .screen_box .screen_div {
            width: 0;
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
        }
        .screen_box .screen_div span {
            width: 48px;
            height: 20px;
            line-height: 20px;
            font-size: 12px;
            color: #999;
            text-align: center;
            display: inline-block;
            margin-right: 8px;
        }
        .screen_box .screen_div span:last-child {
            margin-right: 0;
        }
        .screen_box .screen_div span.active {
            color: #fff;
            background-color: #f3b818;
            border-radius: 5px;
        }
        .screen_box .screen {
            height: 40px;
            display: -webkit-box;
            -webkit-box-align: center;
            padding: 0 15px;
        }
        .screen_box .screen img {
            height: 13px;
            display: block;
            margin-right: 5px;
        }
        .screen_box .screen span {
            color: #999;
            font-size: 12px;
            display: block;
        }
        .screen_box_s {
            padding-top: 21px;
            position: relative;
            z-index: 1000;
        }
        .goods_box {
            background-color: #fff;
        }
        .goods_box ul li .li_box {
            padding: 10px 15px;
            box-sizing: border-box;
            border-bottom: 1px solid #e8e8e8;
        }
        .goods_box ul li:last-child .li_box {
            border-bottom: none;
        }
        .goods_box ul li .li_box .li_boxt {
            display: -webkit-box;
            display: -webkit-flex;
            overflow: hidden;
        }
        .goods_box ul li .li_box .li_boxb {
            padding-top: 10px;
        }
        .goods_box .li_img {
            width: 82px;
            height: 59px;
            border-radius: 5px;
        }
        .goods_box .li_img img {
            height: 100%;
            object-fit: cover;
            width: 82px;
            border-radius: 5px;
        }
        .goods_box .li_info {
            padding-left: 10px;
            box-flex:1;
            -webkit-box-flex:1;
            flex:1;
            -webkit-flex:1;
        }
        .goods_box .li_info .title_box {
            overflow: hidden;
        }
        .goods_box .li_info .title_box .title {
            float: left;
            font-size: 16px;
            color: #333;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .goods_box .li_info .title_box .send {
            float: right;
            color: #999;
            font-size: 12px;
        }
        .goods_box .li_info .evaluate {
            display: -webkit-box;
            display: -webkit-flex;
            align-items: center;
            margin-bottom: 3px;
        }
        .goods_box .li_info .evaluate .evaluate_img {
            overflow: hidden;
            margin-right: 3px;
        }
        .goods_box .li_info .evaluate .evaluate_img img {
            width: 9px;
            float: left;
            margin-right: 3px;
        }
        .goods_box .li_info .evaluate .evaluate_img img:last-child {
            margin-right: 0;
        }
        .goods_box .li_info .evaluate .evaluate_num {
            font-size: 12px;
            margin-right: 14px;
            color: #999;
        }
        .goods_box .li_info .evaluate .month_sell {
            flex: 1;
            -webkit-flex: 1;
            box-flex: 1;
            -webkit-box-flex: 1;
            font-size: 12px;
            color: #999;
        }
        .goods_box .li_info .distance {
            font-size: 12px;
            color: #999;
        }
        .goods_box .li_discount {
            font-size: 12px;
            color: #999;
        }
        .goods_box .li_discount span {
            width: 16px;
            height: 16px;
            line-height: 16px;
            text-align: center;
            background-color: #ff5f78;
            color: #fff;
            display: inline-block;
            border-radius: 3px;
            margin-right: 5px;
        }
        .unable_box {
            text-align: center;
            padding-top: 225px;
        }
        .unable_box img {
            width: 42px;
            vertical-align: middle;
        }
        .unable_box p {
            color: #999;
            font-size: 13px;
            margin: 15px 0 35px;
        }
        .unable_box .reload_btn {
            width: 118px;
            height: 41px;
            line-height: 41px;
            font-size: 16px;
            text-align: center;
            color: #fff;
            background-color: #e62442;
            border-radius: 5px;
            margin: auto;
        }
        .location_off_box {
            background-color: rgba(0, 0, 0, 0.5);
            position: fixed;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            z-index: 999;
            padding: 0 45px;
            display: -webkit-box;
            -webkit-box-align: center;
        }
        .location_off_box .popup {
            background-color: #fff;
            border-radius: 5px;
            overflow: hidden;
        }
        .location_off_box .popup .popup_t {
            height: 50px;
            line-height: 50px;
            color: #333;
            font-size: 16px;
            text-align: center;
            border-bottom: 1px solid #e8e8e8;
        }
        .location_off_box .popup .popup_c {
            padding: 15px;
            color: #999;
            font-size: 13px;
            line-height: 23px;
        }
        .location_off_box .popup .popup_b {
            padding-bottom: 15px;
        }
        .location_off_box .popup .popup_b .btn {
            width: 98px;
            height: 38px;
            line-height: 38px;
            text-align-last: center;
            margin: auto;
            color: #fff;
            background-color: #e62442;
            font-size: 15px;
            border-radius: 5px;
            text-align: center;
        }
        .no_position {
            display: none;
        }
        .has_position {
            display: none;
        }
        .swiper-slide img{
            /*object-fit: none;*/
            /*object-fit: contain;*/
            object-fit: cover;
        }
        .without_data,.without_data_discount{
            display: none;
            color:#ccc;
            text-align:center;
            padding-top: 15px;
            padding-bottom: 15px;
        }
        .without_data2{
            color:#ccc;
            text-align:center;
            padding-top: 15px;
            padding-bottom: 15px;
        }
    </style>
</head>
<body>
    <div class="location_box" id="header">
        <div class="location" tapmode onclick="fnMyLocation();">
            <img src="../image/location3.png">
            <span class="location_name">定位中</span>
        </div>
    </div>
    <div class="has_position">
        <div class="banner">
            <div class="search_wrap">
                <div class="search_box" tapmode onclick="fnSearch();">
                    <img src="../image/search.png">搜索商家
                </div>
            </div>
            <div class="swiper-container">
                <div class="swiper-wrapper" id="swiper-box">

                </div>
            </div>
        </div>
        <div class="nav">
            <ul id="nav_ul">
                <li onclick="" tapmode>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon1.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon2.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon3.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon4.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon5.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon6.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon7.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
                <li>
                    <div class="li_box">
                        <div class="img_box">
                            <img src="../image/nav_icon8.png">
                        </div>
                        <div class="p_box">加载中</div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="discount_box">
            <div class="discount_boxt">
                <img src="../image/discount_icon.png">
                <span>优惠</span>
            </div>
            <div class="discount_boxb" id="discount">

            </div>
        <p class="without_data_discount">暂无附近优惠</p>
        </div>
        <div class="screen_box">
            <div class="location_p">
                <img src="../image/location2.png">
                <span>附近美食</span>
            </div>
            <div class="screen_div" >
                <span class="sort active" onclick="fnSort('distance',this)" tapmode>按距离</span>
                <span class="sort" onclick="fnSort('score',this)" tapmode>按评价</span>
            </div>
            <div class="screen" tapmode onclick="fnSellerScreen();">
                <img src="../image/screen.png">
                <span>筛选</span>
            </div>
        </div>
        <div class="goods_box">
            <ul id="shop_ul">
    <!--             <li tapmode onclick="fnSeller();">
                    <div class="li_box">
                        <div class="li_boxt">
                            <div class="li_img">
                                <img src="../image/temp/1.png">
                            </div>
                            <div class="li_info">
                                <div class="title_box">
                                    <div class="title">商家名称</div>
                                    <div class="send">20元起送</div>
                                </div>
                                <div class="evaluate">
                                    <div class="evaluate_img">
                                        <img src="../image/evaluate_on.png">
                                        <img src="../image/evaluate_on.png">
                                        <img src="../image/evaluate_on.png">
                                        <img src="../image/evaluate_on.png">
                                        <img src="../image/evaluate_off.png">
                                    </div>
                                    <div class="evaluate_num">4.6</div>
                                    <div class="month_sell">月售109单</div>
                                </div>
                                <div class="distance">距离0.43km | 配送费 6元</div>
                            </div>
                        </div>
                        <div class="li_boxb">
                            <div class="li_discount"><span>减</span>满30减10，满40减15</div>
                        </div>
                    </div>
                </li> -->
            </ul>
        <p class="without_data">暂无附近美食</p>
        </div>
    </div>
    <div class="no_position">
        <div class="unable_box">
            <img src="../image/unable.png">
            <p>无法获取地址，请手动定位</p>
            <div class="reload_btn" onclick="fnReloadHome();" tapmode>重新加载</div>
        </div>
<!--         <div class="location_off_box">
            <div class="popup">
                <div class="popup_t">定位服务未开启</div>
                <div class="popup_c">请在设置->隐私->定位服务中开启定位服务，只有知道您的位置才能提供更好的服务~</div>
                <div class="popup_b">
                    <div class="btn">去开启</div>
                </div>
            </div>
        </div> -->
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/swiper.min.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="navTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        {{if(i <= 7){ }}
            <li onclick="fnSellerList({{=i}},'{{=it[i].name}}','{{=it[i].is_package}}')" tapmode>
                <div class="li_box">
                    <div class="img_box">
                        <img src="../image/nav_icon{{=(i+1)}}.png">
                    </div>
                    <div class="p_box">{{=it[i]['name']}}</div>
                </div>
            </li>

        {{ } else{ }}

        {{ }; }}
    {{ }; }}
</script>
<script id="carouseTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <div class="swiper-slide" onclick="fnOpenAd({{=it[i]['object_id']}},'{{=it[i].type}}');" tapmode>
            <img src="../image/default.png">
            <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['pic'])}}">
        </div>
    {{ }; }}
</script>
<script id="discountTpl" type="text/x-dot-template">
    {{if(it['best']['shop_id'] > 0){ }}
        <div class="discount_div" onclick="fnOpenSeller({{=it['best']['shop_id']}})" tapmode>
            <div class="discount_img">
                <img src="{{=formatVideoImg(it['best']['img'])}}">
            </div>
            <div class="discount_p">最佳优惠</div>
        </div>
    {{ }; }}
    {{if(it['today_best']['shop_id'] > 0){ }}
        <div class="discount_div" onclick="fnOpenSeller({{=it['today_best']['shop_id']}})" tapmode>
            <div class="discount_img">
                <img src="{{=formatVideoImg(it['today_best']['img'])}}">
            </div>
            <div class="discount_p">今日优惠</div>
        </div>
    {{ }; }}
    {{if(!it['today_best']['shop_id'] && !it['best']['shop_id']){ }}
        <p class="without_data2">暂无附近优惠</p>
    {{ }; }}
</script>
<script id="activityTpl" type="text/x-dot-template">
{{ for(var i=0;i<it.length;i++){ }}
    <div class="discount_div" onclick="fnOpenActivity('{{=it[i]["key"]}}')" tapmode>
        <div class="discount_img">
            <img src="{{=formatVideoImg(it[i]['value'])}}">
        </div>
        <div class="discount_p">
            {{if(it[i]['key'] == 'best'){ }}
                优惠商家
            {{ } else{ }}
                优惠商品
            {{ }; }}
        </div>
    </div>
{{ }; }}
</script>

<script id="shopTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li onclick="fnOpenSeller({{=it[i]['id']}});" tapmode>
            <div class="li_box">
                <div class="li_boxt">
                    <div class="li_img">
                        <img src="../image/default.png">
                        <img onload="setImg(this);" class="hidden_img" src="{{=formatVideoImg(it[i]['img'])}}">
                    </div>
                    <div class="li_info">
                        <div class="title_box">
                            <div class="title">{{=it[i]['name']}}</div>
                            <div class="send">RM{{=it[i]['start_delivery']}}起送</div>
                        </div>
                        <div class="evaluate">
                            <div class="evaluate_img">
                                {{if(it[i]['full_star'] > 0){  }}
                                    {{ for(var j=0;j<it[i]['full_star'];j++){ }}
                                        <img src="../image/evaluate_on.png">
                                    {{ }; }}
                                {{ }; }}
                                {{if(it[i]['half_star'] > 0){  }}
                                    <img src="../image/evaluate_half.png">
                                {{ }; }}
                                {{if(it[i]['off_star'] > 0){  }}
                                    {{ for(var j=0;j<it[i]['off_star'];j++){ }}
                                        <img src="../image/evaluate_off.png">
                                    {{ }; }}
                                {{ }; }}

                            </div>
                            <div class="evaluate_num">{{=it[i]['score_format']}}</div>
                            <div class="month_sell">月售{{=it[i]['month_sale']}}单</div>
                        </div>
                        <div class="distance">距离{{=it[i]['distance']}}km | 配送费 RM{{=it[i]['express']}}</div>
                    </div>
                </div>
                <div class="li_boxb">
                    {{if(it[i]['coupon_str']){ }}
                        <div class="li_discount"><span>减</span>{{=it[i]['coupon_str']}}</div>
                    {{ }; }}
                </div>
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">

    var swiper = false; // 定义swiper
    var canLoad = false;

    apiready = function(){
        $api.fixStatusBar(header);
        getCarouse();
        fnGetMenuData();
        p = 1;
        coupon = '';
        ticket = '';
        price = '';
        shop_type = '';
        order = 'distance';
        lat = '';
        lng = '';
        // openLoading();
        // fnGetCoupon();
        fnGetActivityCover();
        api.addEventListener({
            name: 'homeScreen'
        }, function(ret, err) {
            $('.screen_box').removeClass('screen_box_s');
            $(window).scrollTop($('.goods_box').offset().top - $('.search_wrap').height() - $('.screen_box').height());
        });

        window.addEventListener('scroll', function(event) {
            searchFixed();
            // navFixed();
        }, false);
        document.addEventListener("touchmove", function(event) {
            searchFixed();
            // navFixed();
        });

        //定位功能
        fnGetLocation();

        //后台进入前台也
        api.addEventListener({
            name:'resume'
        }, function(ret, err){
            // fnGetLocation();
        });

        //监听筛选
        api.addEventListener({
            name: 'choseAttrEvent'
        }, function(ret, err) {
            if (ret) {
                coupon = ret.value.coupon;
                ticket = ret.value.ticket;
                price = ret.value.price;
                shop_type = ret.value.shop_type;
                p = 1;
                // openLoading();
                // fnGetCoupon();
                fnGetShop(1);
            }else {

            }
        });

        // 下拉刷新
        api.setCustomRefreshHeaderInfo({
            bgColor: '#f8f8f8',
            loadAnimInterval:60,
            image: {
               pull: pullArr,
               load: loadArr
            }
        }, function(ret, err) {
            if(lat && lng) {
                p = 1;
                getCarouse();
                fnGetMenuData();
                fnGetActivityCover();
                if(lat && lng){
                    // fnGetCoupon();
                }
                coupon = '';
                price = '';
                ticket = '';
                shop_type = '';
                order = 'distance';
                fnGetShop(1);
            }else {
                setTimeout(function(){
                    api.refreshHeaderLoadDone();
                },500)
            }
        });

        // 上拉加载
        api.addEventListener({
            name:'scrolltobottom',
            extra:{
                threshold:0            //设置距离底部多少距离时触发，默认值为0，数字类型
            }
        }, function(ret, err){
            if(canLoad && canLoad == true){
                p++;
                fnGetShop();
            }
        });

        //监听坐标变化
        api.addEventListener({
            name: 'pointIsChangeEvent'
        }, function(ret, err) {
            if (ret) {
                lng = ret.value.lng;
                lat = ret.value.lat;
                p = 1;
                coupon = '';
                price = '';
                shop_type = '';
                ticket = '';
                order = 'distance';
                fnGetShop(1);
                fnGetLocationName();
            }
        });
    };

    function fnOpenActivity (_key) {
        api.openWin({
            name: 'activity_'+_key,
            url: 'activity_'+_key+'.html',
            pageParam:{
                lat : lat,
                lng : lng
            }
        });
    };

    function fnGetLocation() {
        var GoogleMap = api.require('googleMap');
        var hasLocation = false;
        GoogleMap.getLocation(function(ret) {
            if (ret.status) {
                hasLocation = true;
                $('.has_position').show();
                $('.no_position').hide();
                lat = ret.lat;
                lng = ret.lon;
                $api.setStorage('now_lat',lat);
                $api.setStorage('now_lng',lng);
                fnGetShop();
                // fnGetCoupon();
                fnGetLocationName();
            }else {
                $('.has_position').hide();
                $('.no_position').show();
            }
        });
        if(hasLocation == false) {
            $('.has_position').hide();
            $('.no_position').show();
        }
        closeLoading();
    }

    function fnGetLocationName() {
        api.ajax({
            url: 'https://maps.googleapis.com/maps/api/geocode/json?language=zh-CN&latlng='+lat+','+lng+'&key=AIzaSyAdD-aDbsplFDJzsIFESWLnlBE7DFcZUWk',
            method: 'get',
            data: {
                values: {
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret['results'] && ret['results'][0]['place_id']) {
                    fnGetNameByPlace(ret['results'][0]['place_id']);
                }else {
                    $('.location_name').text('定位失败');
                }
            }
        });
    }

    function fnGetNameByPlace(place_id) {
        api.ajax({
            url: 'https://maps.googleapis.com/maps/api/place/details/json?placeid='+place_id+'&key=AIzaSyAdD-aDbsplFDJzsIFESWLnlBE7DFcZUWk&language=zh-CN',
            method: 'get',
            data: {
                values: {
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 'OK') {
                    $('.location_name').text(ret['result']['name']);
                }
            } else {
            }
        });
    }

    // 搜索置顶
    function searchFixed() {
        if ($('.search_wrap')[0].getBoundingClientRect().top <= 0) {
            if (!$('.search_wrap').hasClass('search_fixed')) {
                $('.search_wrap').addClass('search_fixed');
            }
        }
        if ($('.banner')[0].getBoundingClientRect().top - 41 > 0) {
            if ($('.search_wrap').hasClass('search_fixed')) {
                $('.search_wrap').removeClass('search_fixed');
            }
        }
    }

    // 筛选置顶
    // function navFixed() {
    //     if ($('.screen_box')[0].getBoundingClientRect().top <= 0) {
    //         if (!$('.screen_box').hasClass('screen_box_fixed')) {
    //             $('.screen_box').addClass('screen_box_fixed');
    //             $('.goods_box').css('paddingTop', $('.screen_box').height());
    //         }
    //     }
    //     if ($('.goods_box')[0].getBoundingClientRect().top > 0) {
    //         if ($('.screen_box').hasClass('screen_box_fixed')) {
    //             $('.screen_box').removeClass('screen_box_fixed');
    //             $('.goods_box').css('paddingTop', 0);
    //         }
    //     }
    // }

    checkTouch('swiper-box','touch_dire');
    function checkTouch(id_,direct) {
        var pagX = 0;
        var pagY = 0;
        document.getElementById(id_).addEventListener('touchstart', function (ev) {
            pagX = ev.touches[0].clientX;
            pagY = ev.touches[0].clientY;
        }, false);

        document.getElementById(id_).addEventListener('touchmove', function (ev) {
            var new_pagX = ev.touches[0].clientX;
            var new_pagY = ev.touches[0].clientY;
            // 判断是否垂直
            if (Math.abs(new_pagY - pagY) > 2*Math.abs(new_pagX - pagX)) {
                window[direct] = 'y';
            } else {
                window[direct] = 'x';
            }
        }, false);
    }

    // 轮播图
    function goSwiper() {
        if(swiper){
            swiper.destroy();
        }
        swiper = new Swiper('.banner .swiper-container', {
            slidesPerView: 1,
            loop: true,
            onTouchStart: function(swiper){
                if(window.touch_dire == 'x') {
                    api.setFrameAttr({
                        name: api.frameName,
                        bounces: false
                    });
                }
            },
            onTouchEnd: function() {
                api.setFrameAttr({
                    name: api.frameName,
                    bounces: true
                });
            }
        });
    }

    // 搜索
    function fnSearch() {
        api.openWin({
            name: 'search',
            url: 'search.html'
        });
    }

    // 我的位置
    function fnMyLocation() {
        api.openWin({
            name: 'my_location',
            url: 'my_location.html'
        });
    }

    // 商品详情
    function fnSeller(sid) {
        api.openWin({
            name: 'seller',
            url: 'seller.html',
            pageParam : {
                sid : sid
            }
        });
    }

    function evUpdateUser (_id,_token) {
        if(_id > 0){
            // 是登录
            api.ajax({
                url: rootPath+'/api/user/get_user_info',
                method: 'get',
                data: {
                    values: {
                        uid: _id,
                        token:_token
                    },
                }
            }, function(ret, err) {
                if (ret) {
                    if(ret.status == 200) {
                        $api.setStorage('user_data', ret.data);
                    }
                }
            });
        }else{
            // 是退出登录
            $api.setStorage('user_data', null);
        }
    };

    //店铺列表
    function fnSellerList(k,title,is_package) {
        if(is_package >= 1){
            // 是包裹，判断是否登录
            var user = $api.getStorage('user_data');
            if(!user) {
                api.openWin({
                    name: 'login',
                    url: './login.html',
                });
                return;
            }else{
                // 已经登录了，则打开送包裹页面
                api.openWin({
                    name: 'package',
                    url: 'package.html'
                });
            }
        }else{
            if(menu_data[k]['sub'].length > 0) {
                api.openWin({
                    name: 'seller_list',
                    url: './seller_list.html',
                    pageParam: {
                        title : title,
                        sub: menu_data[k]['sub']
                    }
                });
            }
        }
    }

    //获取菜单数据
    menu_data = {};
    function fnGetMenuData() {
        api.ajax({
            url: rootPath+'/api/data/app_cats',
            method: 'get',
            cache:true,
            data: {
                values: {
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200) {
                    menu_data = ret.data;
                    $('#nav_ul').empty();
                    appendDotData(menu_data,'navTpl','nav_ul',true,false);
                }
            } else {

            }
        });
    }

    function fnGetShop(type) {
        api.ajax({
            url: rootPath+'/api/shop/get_shops',
            method: 'get',
            cache: true,
            timeout:10,
            data: {
                values: {
                    p:p,
                    order : order,
                    page_num:10,
                    price : price,
                    coupon:coupon,
                    ticket:ticket,
                    shop_type : shop_type,
                    lat:lat,
                    lng:lng
                },
            }
        }, function(ret, err) {
          console.log('获取到的店铺===='+ JSON.stringify(ret));
          console.log('获取到的店铺===='+ JSON.stringify(err));
            //type=1 下拉刷新，清除原来数据
            if(type == 1) {
                $('#shop_ul').empty();
            }
            if (ret) {
                if(ret.status == 200) {
                    $('.without_data').hide();
                    fnSetShop(ret.data);
                    if(ret.data.length == 10){
                        canLoad = true;
                        p++;
                    }
                }else {
                    if(p == 1) {
                        $('.without_data').show();
                    }
                }
            } else {
                api.toast({
                    msg: err.msg,
                    location : 'middle'
                });
            }
            if(type == 1) {
                // 是下拉刷新的，要收回
                api.refreshHeaderLoadDone();
            }
            if(type == 2) {
                //上拉给点假时间,好看些
                setTimeout(function(){
                    $(".loading").hide();
                },1000);
            }else {
                $('.loading').hide();

            }
            closeLoading();
        });
    }

    function fnSetShop(data_) {
        var listT = $api.byId('shopTpl');
        var listTFn = doT.template(listT.text);
        var html = listTFn(data_);
        var list = $api.byId('shop_ul');
        $api.append(list, html);
        api.parseTapmode();
    }

    function fnSort(sort,this_) {
        if(order != sort) {
            // openLoading();
            $('.sort').removeClass('active');
            $(this_).addClass('active');
            order  = sort;
            p = 1;
            fnGetShop(1);
            // if ($('.screen_box').hasClass('screen_box_fixed')) {
            //     $(window).scrollTop($('.goods_box').offset().top + 1);
            // }
        }
    }

    function fnSellerScreen() {
        $(window).scrollTop($('.goods_box').offset().top - $('.screen_box').height());
        $('.screen_box').addClass('screen_box_s');
        api.openFrame({
            name: 'home_screen_bg',
            url: 'home_screen_bg.html',
            rect: {
                x: 0,
                y: 0,
                w: 'auto',
                h: 'auto'
            },
            bounces: false
        });
        api.openFrame({
            name: 'home_screen_mask',
            url: 'home_screen_mask.html',
            rect: {
                x: 0,
                y: '61px',
                w: 'auto',
                h: api.winHeight - 61
            },
            bounces: false
        });
        api.openFrame({
            name: 'home_screen',
            url: 'home_screen.html',
            rect: {
                x: 0,
                y: '61px',
                w: 'auto',
                h: '400px'
            },
            bounces: false,
            pageParam: {
                coupon : coupon,
                price : price,
                shop_type : shop_type,
                ticket: ticket
            }
        });
    }

    function getCarouse() {
        api.ajax({
            url: rootPath+'/api/data/get_carouse',
            method: 'get',
            cache:true,
            data: {
                values: {
                },

            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200) {
                    $('#swiper-box').empty();
                    appendDotData(ret.data,'carouseTpl','swiper-box',true,false);
                    goSwiper();
                }
            } else {

            }
        });
    }

    function fnGetActivityCover () {
        api.ajax({
            url: rootPath+'/api/data/activity_cover',
            method: 'get',
            cache: true,
        }, function(ret, err) {
            if(ret && ret.status == 200) {
                setDotData(ret.data,'activityTpl','discount',true);
            }
        });
    };

    function fnGetCoupon() {
        api.ajax({
            url: rootPath+'/api/data/index_coupon',
            method: 'get',
            cache: true,
            data: {
                values: {
                    lat:lat,
                    lng:lng
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200) {
                    if(ret.data.best || ret.data.today_best) {
                        setDotData(ret.data,'discountTpl','discount',true);
                    }else{
                        $(".without_data_discount").show();
                    }
                }
            } else {

            }
        });
    }

    function fnReloadHome() {
        var GoogleMap = api.require('googleMap');
        var hasLocation = false;
        GoogleMap.getLocation(function(ret) {
            if (ret.status) {
                hasLocation = true;
                $('.has_position').show();
                $('.no_position').hide();
                lat = ret.lat;
                lng = ret.lon;
                $api.setStorage('now_lat',lat);
                $api.setStorage('now_lng',lng);
                fnGetShop();
                fnGetActivityCover();
                // fnGetCoupon();
                fnGetLocationName();
            }else {
                $('.has_position').hide();
                $('.no_position').show();
                api.toast({
                    msg: '获取失败,请先开启定位',
                    location:'middle'
                });
            }
        });
        if(hasLocation == false) {
            $('.has_position').hide();
            $('.no_position').show();
            api.toast({
                msg: '获取失败,请先开启定位',
                location:'middle'
            });
        }

    }

    //打开广告
    function fnOpenAd(object_id,type) {
        if(type == 'shop') {
            fnOpenSeller(object_id);
        }
    }
</script>
</html>
